{extend name="public/base"}
{block name='main'}
<!-- 二级页次级导航结束 -->
<!-- 广告 -->
<div class="layui-container mb20">
  <div class="banner-wrap"><!--矩形广告位-->
  </div>
</div>
<!-- 广告 end-->
<!-- 主体开始 -->
<div class="layui-container">
  <div class="layui-row layui-col-space15">
    <!-- 左侧开始 -->
    <div class="layui-col-md9 content detail">
      <div class="fly-panel fly-panel-user" pad20>
        <div class="layui-tab layui-tab-brief" >
          <ul class="layui-tab-title">
            <li class="layui-this"><a href="{:url('doc/doc_share')}">上传单个文档</a></li>
          </ul>
        </div>
        <!-- 上传部分开始 -->
        <form method="post" class="layui-form layui-form-pane" enctype="multipart/form-data">
          <input type="text" name="fileurl"  id="fileurl"  size="30" value="{$info.fileurl|default=''}">
          <input type="text" name="ext"  id="ext"  size="30" value="{$info.ext|default=''}">
          <input type="text" name="filesize"  id="filesize"  size="30" value="{$info.filesize|default=''}">
          <input type="text" name="oldname"  id="oldname"  size="30" value="{$info.oldname|default=''}">
          <input type="text" name="hash"  id="hash"  size="30" value="{$info.hash|default=''}">
          <input type="text" name="viewurl"  id="viewurl"  size="30" value="">
          <input type="text" name="convert_key"  id="convert_key"  size="30" value="">
          <input type="text" name="cid" id="J_cate_id" value="" />
          <div class="layui-form-item" style="margin-top: 15px;">
            <div class="layui-input-block" style="margin-left:0px;">
              <div class="upfile">
                <div class="layui-upload-drag" id="upfile">
                  <i class="layui-icon"></i>
                  <p>点击上传，或将文件拖拽到此处</p>
                </div>
                <div class="layui-progress" lay-showpercent="true" lay-filter="demo" >
                  <div class="layui-progress-bar" lay-percent=""></div>
                </div>
              </div>
            </div>
          </div>
          <div class="layui-form-item" style="margin-top: 15px;">
            <label class="layui-form-label">文档标题</label>
            <div class="layui-input-block">
              <input type="text" class="layui-input" name="title" id="title" lay-verify="title"  placeholder="请填写文档标题！">
            </div>
          </div>
          <div class="layui-form-item" >
            <label class="layui-form-label">文档售价</label>
            <div class="layui-input-block">
              <input type="number" class="layui-input" name="score" id="score" value="0" placeholder="请填写售价，只能是数字！">
            </div>
          </div>
          <div class="layui-form-item" >
            <label class="layui-form-label">文档标签</label>
            <div class="layui-input-block">
              <input type="text" class="layui-input" name="tags" id="tags" lay-verify="title" placeholder="可填写多个,以英文都好分割！" >
              <span class="layui-btn layui-btn-xs layui-bg-blue" id="J_gettags" style="margin-top: 5px;">自动获标签</span>
            </div>
          </div>
          <div class="layui-form-item" >
            <label class="layui-form-label">文档简介</label>
            <div class="layui-input-block">
              <textarea placeholder="请输入文档简介" name="intro" class="layui-textarea"></textarea>
            </div>
          </div>
          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">文档分类</label>
              <div class="layui-input-inline">
                <select id="groupcateid" class="J_cate_select" multiple="multiple" lay-ignore >
                  <option disabled>请选择一级分类</option>
                  {volist name="catlist" id="vo"}
                    <option value="{$vo.id}">{$vo.name}</option>
                  {/volist}
                </select>
              </div>
            </div>
            <div class="layui-inline">
              <div class="layui-input-inline">
                <select name="tid" id="tid" class="J_cate_select" multiple="multiple" lay-ignore>
                  <option disabled>请选择二级分类</option>
                </select>
              </div>
            </div>
          </div>
          <div class="layui-form-item" >
            <div class="layui-input-block">
              <a class="layui-btn" lay-submit="" lay-filter="upbtn"> 发布文档 </a>
              <span style="float: right;">
                      <input type="checkbox" id="agree" name="like1[write]" lay-skin="primary" checked="checked">
                     <a href="{:url('page/index',array('id'=>6))}" target="_blank" style="position: relative; top: 4px; left: 5px; color: #999;">同意文档发布协议</a>
                    </span>
            </div>
          </div>


        </form>
        <!-- 上传部分结束 -->
      </div>
    </div>
    <!-- 左侧结束 -->

    <!-- 右侧开始 -->
    <div class="layui-col-md3 ">
      <!-- 客服开始 -->
      <div class="fly-panel">
        <h3 class="fly-panel-title">遇到问题，联系客服免费帮您解决</h3>
        <div class="fly-panel-main">
          <a target="_blank" href="1111" class="fly-zanzhu">联系QQ客服:123456</a>
          <h3 style="margin-top: 10px; text-align: center;">全国免长途电话：123444</h3>
        </div>
      </div>
      <!-- 客服结束 -->

    </div>
    <!-- 右侧结束 -->
  </div>
</div>

<script>
  //主动加载jquery模块
  layui.use(['jquery', 'layer', 'form', 'upload', 'element'],
          function() {
            var $ = layui.$,
               layer = layui.layer,
                    form = layui.form,
                    upload = layui.upload,
                    element = layui.element;

            // 创建监听函数
            var xhrOnProgress = function(fun) {
              xhrOnProgress.onprogress = fun; //绑定监听
              //使用闭包实现监听绑
              return function() {
                //通过$.ajaxSettings.xhr();获得XMLHttpRequest对象
                var xhr = $.ajaxSettings.xhr();
                //判断监听函数是否为函数
                if (typeof xhrOnProgress.onprogress !== 'function') return xhr;
                //如果有监听函数并且xhr对象支持绑定时就把监听函数绑定上去
                if (xhrOnProgress.onprogress && xhr.upload) {
                  xhr.upload.onprogress = xhrOnProgress.onprogress;
                }
                return xhr;
              }
            }

            //执行实例
            var uploadInst = upload.render({
              elem: '#upfile', //绑定元素              ,
              field: 'Filedata',
              url: '{$uploadimgurl}', //上传接口
              accept: 'file', //普通文件
              exts: 'doc|docx|xls|xlsx|pptx|ppt|wps|et|pdf|txt', //允许后缀
              xhr: xhrOnProgress,
              choose: function(obj) {
                element.progress('demo', value = '0%');
              },
              progress: function(value) {
                $('.layui-progress-bar').removeClass('layui-bg-red');
                element.progress('demo', value + '%') //设置页面进度条
              },
              done: function(data) {
                //上传完毕回调
                if (data.status == 1) {
                  layer.msg('上传成功', {
                            time: 1800
                          },
                          function() {
                            $('#fileurl').val(data.info.name);
                            $('#oldname').val(data.info.oldname);
                            $('#title').val(data.info.oldname);
                            $('#upinfo').html(data.info.oldname + "." + data.info.ext);
                            $('#filesize').val(data.info.size);
                            $('#viewurl').val(data.info.name);
                            $('#hash').val(data.info.hash);
                            $('#tags').val(data.info.ext);
                            $('#ext').val(data.info.ext);
                            $('#convert_key').val(data.info.convert_key);
                            $('#J_gettags').click();
                            element.progress('demo', value = '100%');
                            $('.dengdai').addClass('hidden');
                            $('.shibai').addClass('hidden');
                            $('.chenggong').removeClass('hidden');
                            $('.chenggong p').html(data.info.oldname + '上传成功');
                          });
                } else {
                  layer.msg(data.info, {
                    time: 1800
                  });
                  $('.layui-progress-bar').addClass('layui-bg-red');
                  $('.dengdai').addClass('hidden');
                  $('.shibai').removeClass('hidden');
                  $('.shibai p').html(data.info);
                  $('#oldname').val();
                }
              },
              error: function() {
                layer.msg("网络异常，请尝试重新上传！", {
                  time: 1800
                });
                element.progress('demo', value = '网络异常，请尝试重新上传');
                $('.layui-progress-bar').addClass('layui-bg-red');
              }
            });

            //获取自动标签
            layui.$('#J_gettags').on('click',
                    function() {
                      var title = $.trim($('#title').val());
                      if (title == '') {
                        layer.msg("请先上传文档，或输入文档标题！", {
                          time: 1800
                        });
                        return false;
                      }
                      $.getJSON('{:url("doc/ajax_gettags")}', {
                                title: title
                              },
                              function(result) {
                                if (result.status == 1) {
                                  $('#tags').val(result.data);
                                } else {
                                  $.wkcms.tip({
                                    content: result.msg
                                  });
                                }
                              });
                    });

            //点一级获二级分类
            $('#groupcateid').change(function() {
              loading = layer.msg('请稍后...', {
                icon: 16,
                shade: 0.01
              });
              var caturl = '{:url("doc/ajax_getsubcate")}?id=' + $(this).val();
              $('#J_cate_id').val($(this).val()); //赋值选中ID
              $.post(caturl,
                      function(data) {
                        layer.close(loading);
                        if (data.status == 1) {
                          $('#tid').html('');
                          $('#tid').append('<option disabled>请选择二级分类</option>');
                          $('#tiddiv').removeClass("disnone");
                          for (var i = 0; i < data.data.length; i++) {
                            $('#tid').append('<option value="' + data.data[i]['id'] + '">' + data.data[i]['name'] + '</option>');
                          }
                        } else {
                          //$('#tiddiv').addClass("disnone");
                          $('#tid').html('');
                        }

                      });
            });
            //点二级获三级分类
            $('#tid').change(function() {
              $('#J_cate_id').val($(this).val()); //赋值选中ID
              var caturl = '{:url("doc/ajax_getsubcate")}?id=' + $(this).val();

            });

            //提交
            form.on('submit(upbtn)',
                    function(data) {
                      var agree = $('#agree').is(":checked");
                      if (!agree) {
                        layer.msg("请先同意并阅读协议", {
                          time: 1800
                        });
                        return false;
                      }
                      var title = $.trim($('#title').val());
                      if (title == '') {
                        layer.msg("请先上传文档", {
                          time: 1800
                        });
                        return false;
                      }
                      // 提交到方法 默认为本身
                      if ($('#J_cate_id').val() == '') {
                        layer.msg("请选择文档分类", {
                          time: 1800
                        });
                        return false;
                      }

                      loading = layer.msg('正在提交，请稍后...', {
                        icon: 16,
                        shade: 0.01
                      });
                      $.post("{:url('doc/doc_share',array('type'=>'add'))}", data.field,
                              function(res) {
                                layer.close(loading);
                                if (res.status == 1) {
                                  layer.msg(res.msg, {
                                            time: 1800
                                          },
                                          function() {
                                            location.href = "{:url('ucenter/mydoclist')}";
                                          });
                                } else {
                                  layer.msg(res.msg, {
                                    time: 1800
                                  });
                                }
                              });
                      return true;
                    });
          });

</script>
<script>
  layui.config({
    version: "3.0.0"
    ,base: '__PUBLIC__/theme/blueideal/mods/' //这里实际使用时，建议改成绝对路径
  }).extend({
    fly: 'index'
  }).use('fly');
</script>
<!-- 主体结束 -->
{/block}
